class Solution {
public:
    long long countAlternatingSubarrays(vector<int>& nums)
    {
        long long ans = 0;
        int right = 0;
        int left = 0;
        int n = nums.size();
        while (right < n)
        {
            if (right == left) ans += 1;
            else if (right > left && nums[right] != nums[right - 1]) ans += right - left + 1;
            else
            {
                left = right;
                right--;
            }
            right++;
        }
        //1 2 3 4 5 6 7 8 9 10     1 2 3 4 5 6
        return ans;
    }
};